﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Text;

namespace AIC_PN
{
    public partial class BaoCaoTongHopTanXuatKhenThuong : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadKyBaoCao();
            }
        }

        protected void LoadKyBaoCao()
        {
            string sql = "SELECT * FROM dbo.tblDanhMuc WHERE MaDM = 'KyBaoCao'";
            DataTable dt = SqlHelper.ExecuteDataTable(CommandType.Text, sql);
            chkKyBaoCao.DataSource = dt;
            chkKyBaoCao.DataTextField = "TenDM";
            chkKyBaoCao.DataValueField = "ID";
            chkKyBaoCao.DataBind();
        }

        protected void btnTongHop_Click(object sender, EventArgs e)
        {
            string listKyBaoCao = "";
            for (int i = 0; i < chkKyBaoCao.Items.Count; i++)
            {
                if (chkKyBaoCao.Items[i].Selected)
                {
                    listKyBaoCao += chkKyBaoCao.Items[i].Value + ",";
                }
            }
            string sql = "SELECT bc.CanBoID ,dv.TenDV,cb.TenCanBo , " +
                        "TenDM = STUFF((SELECT ','+TenDM FROM dbo.tblDanhMuc WHERE bc.KyBaoCaoID_DM = dbo.tblDanhMuc.ID FOR XML PATH('')) , 1 , 1 , '' ) " +
                        "FROM dbo.tblBaoCaoMau11 bc INNER JOIN dbo.tblCanBo cb ON bc.CanBoID = cb.ID INNER JOIN dbo.tblDonVi dv ON cb.DonViID = dv.ID " +
                        "WHERE bc.KyBaoCaoID_DM IN ( " + listKyBaoCao.Substring(0, listKyBaoCao.Length - 1) + " ) GROUP BY bc.CanBoID,TenCanBo,TenDV,KyBaoCaoID_DM ";
            DataTable dt = SqlHelper.ExecuteDataTable(CommandType.Text, sql);
            grvData.DataSource = dt;
            grvData.DataBind();
        }

        protected void btnIn_Click(object sender, EventArgs e)
        {
            string listKyBaoCao = "";
            for (int i = 0; i < chkKyBaoCao.Items.Count; i++)
            {
                if (chkKyBaoCao.Items[i].Selected)
                {
                    listKyBaoCao += chkKyBaoCao.Items[i].Value + ",";
                }
            }
            string sql = "SELECT bc.CanBoID ,dv.TenDV,cb.TenCanBo , " +
                        "TenDM = STUFF((SELECT ','+TenDM FROM dbo.tblDanhMuc WHERE bc.KyBaoCaoID_DM = dbo.tblDanhMuc.ID FOR XML PATH('')) , 1 , 1 , '' ) " +
                        "FROM dbo.tblBaoCaoMau11 bc INNER JOIN dbo.tblCanBo cb ON bc.CanBoID = cb.ID INNER JOIN dbo.tblDonVi dv ON cb.DonViID = dv.ID " +
                        "WHERE bc.KyBaoCaoID_DM IN ( " + listKyBaoCao.Substring(0, listKyBaoCao.Length - 1) + " ) GROUP BY bc.CanBoID,TenCanBo,TenDV,KyBaoCaoID_DM ";
            DataTable dt = SqlHelper.ExecuteDataTable(CommandType.Text, sql);
            grvData.DataSource = dt;
            grvData.DataBind();


            grvData.AllowPaging = false;
            grvData.DataBind();

            Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction", "PrintPage()", true);
        }

        protected void grvData_PreRender(object sender, EventArgs e)
        {
            for (int rowIndex = 0; rowIndex < grvData.Rows.Count - 2; rowIndex++)
            {
                GridViewRow row = grvData.Rows[rowIndex];
                GridViewRow previousRow = grvData.Rows[rowIndex + 1];

                if (row.Cells[0].Text == previousRow.Cells[0].Text && row.Cells[1].Text == previousRow.Cells[1].Text)
                {
                    row.Cells[0].RowSpan = previousRow.Cells[0].RowSpan < 2 ? 2 : previousRow.Cells[0].RowSpan + 1;
                    row.Cells[1].RowSpan = previousRow.Cells[1].RowSpan < 2 ? 2 : previousRow.Cells[1].RowSpan + 1;
                    previousRow.Cells[2].Text += "; " + row.Cells[2].Text;
                    row.Cells[0].Visible = false;
                    row.Cells[1].Visible = false;
                    row.Cells[2].Visible = false;
                }
            }
        }
    }
}
